package com.nf.health.app.mine.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.CommentAdapter;
import com.nf.health.app.adapter.GridViewAdapter;
import com.nf.health.app.adapter.HealthSchemeAdapter;
import com.nf.health.app.customview.MyGridView;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.models.CommentVo;
import com.nf.health.app.models.HealthSchemeVo;
import com.nf.health.app.models.MapVo;
import com.nf.health.app.models.SchemeVo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.Util;
import com.nf.health.app.utils.ViewUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 健康详情页面
 * Created by Administrator on 2016/3/23.
 */
public class HealthSchemeActivity extends BaseActivity {


    List<HealthSchemeVo> mlist;


    Map<String, String> map2 = new TreeMap<String, String>();

    List<MapVo> mlist_gridview;

    List<String> list = new ArrayList<>();
    @Bind(R.id.jtjh_lin)
    LinearLayout jtjhLin;


    @Bind(R.id.down_lin)
    LinearLayout downLin;//底部按钮

    private CommentAdapter commentAdapter;//评论适配器
    List<CommentVo> commentVos;
    @Bind(R.id.commet_btn)//评价按钮
            Button commetBtn;
    @Bind(R.id.add_btn)//重新添加按钮
            Button addBtn;

    private HealthSchemeAdapter healthSchemeAdapter;

    String detail, totaldaynum, daynum, name, id, planids, everydaynum, schemeid, schemetempletid;
    @Bind(R.id.hs_title_name)//名称
            TextView hsTitleName;
    @Bind(R.id.hs_tiele_bfb)//百分百
            TextView hsTieleBfb;
    @Bind(R.id.hs_tiele_ts)//天数
            TextView hsTieleTs;
    @Bind(R.id.hs_tv_ms)//描述
            TextView hsTvMs;
    @Bind(R.id.ji_listview)//listview
            ListView ji_listview;

    @Bind(R.id.comment_listview)//评论listview
            MyListView comment_listview;


    @Bind(R.id.edit_health_scheme)//评论输入框
            EditText edit_health_scheme;
    @Bind(R.id.health_comment_lin)//评论布局
            LinearLayout health_comment_lin;
    @Bind(R.id.fasong_btn)//评论回复按钮
            Button fasong_btn;


    String week;


    private int currentPos = -1;


    TextView textView;

    LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();

    int page = 1;
    int pagesize = 20;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_healthscheme);
        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "健康方案");

        detail = getIntent().getStringExtra("detail");//描述
        totaldaynum = getIntent().getStringExtra("totaldaynum");//共有
        daynum = getIntent().getStringExtra("daynum");//已完成
        name = getIntent().getStringExtra("name");//名称
        planids = getIntent().getStringExtra("planids");//planids
        everydaynum = getIntent().getStringExtra("everydaynum");//everydaynum
        id = getIntent().getStringExtra("id");//id
        schemetempletid = getIntent().getStringExtra("schemetempletid");//健康列表id


        addBtn.setOnClickListener(this);
        commetBtn.setOnClickListener(this);
        fasong_btn.setOnClickListener(this);//评论回复按钮

        provider.requestHealthScheme(id, "HealthScheme");
        provider.requestIdHealthSchemeUserList(id,"Get_HealthScheme");//我的医嘱  根据方案id 获取详情

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {
            case R.id.commet_btn://评价 点击事件
                health_comment_lin.setVisibility(View.VISIBLE);

                break;
            case R.id.add_btn://添加健康事件

                provider.requestAddHealthScheme(schemetempletid, "AddHealth");

                break;
            case R.id.fasong_btn://评论恢复按钮

                String value = edit_health_scheme.getText().toString();
                if (TextUtils.isEmpty(value)) {
                    showToast("评论不可为空");
                } else {

                    provider.requestSchemeComment(schemetempletid, value, "SchemeComment");
                }

                break;
        }
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (actionName.equals("HealthScheme")) {


            mlist = (List<HealthSchemeVo>) object;

            jtjhLin.removeAllViews();

            for (int i = 0; i < mlist.size(); i++) {
                View view = View.inflate(this, R.layout.item_health_scheme_item, null);

                TextView chongfu = (TextView) view.findViewById(R.id.chongfu);//重复运动周期

                TextView health_scheme_name = (TextView) view.findViewById(R.id.health_scheme_name);//具体计划名称
                TextView health_scheme_ts = (TextView) view.findViewById(R.id.health_scheme_ts);//具体计划天数
                LinearLayout lin_dier = (LinearLayout) view.findViewById(R.id.lin_dier);//具体计划重复天数

                LinearLayout lin_disan = (LinearLayout) view.findViewById(R.id.lin_disan);//具体计划点击展开
                LinearLayout lin_disan2 = (LinearLayout) view.findViewById(R.id.lin_disan2);//具体计划点击收藏

                final LinearLayout lin_yi = (LinearLayout) view.findViewById(R.id.lin_yi);//点击展开显示的布局

                final LinearLayout lin_wei = (LinearLayout) view.findViewById(R.id.lin_wei);//点击展开隐藏的布局

                final MyGridView health_scheme_gridview = (MyGridView) view.findViewById(R.id.health_scheme_gridview);//点击展开隐藏的布局


                final Button today_btn = (Button) view.findViewById(R.id.today_btn);//今日完成按钮

                final LinearLayout lin_xx = (LinearLayout) view.findViewById(R.id.lin_xx);//方案详情

                final TextView yi_health_scheme_name = (TextView) view.findViewById(R.id.yi_health_scheme_name);//展开后的方案名称

                final TextView health_scheme_ts2 = (TextView) view.findViewById(R.id.health_scheme_ts2);//展开后的完成百分之
                health_scheme_name.setText(mlist.get(i).getName());//名称赋值
                yi_health_scheme_name.setText(mlist.get(i).getName());//名称赋值

                int a = Integer.parseInt(mlist.get(i).getDaynum());
                final int b = Integer.parseInt(mlist.get(i).getTotaldaynum());

                final int c = a * 100 / b;

                health_scheme_ts.setText(c + "%");
                health_scheme_ts2.setText(a + "/" + b);
                //赋值gridview

                mlist_gridview = new ArrayList<MapVo>();
                map = mlist.get(i).getSchedule();

                SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);

                if(map.get(str)!=null) {
                    if (map.get(str).equals("2")) {
                        today_btn.setTextColor(Color.GRAY);
                        today_btn.setBackgroundResource(R.drawable.bg_drawer);
                    }
                }
                for (String key : map.keySet()) {

                    MapVo mapVo = new MapVo(map.get(key), key);
                    mlist_gridview.add(mapVo);
                }
                Collections.sort(mlist_gridview, new Comparator<MapVo>() {
                    @Override
                    public int compare(MapVo lhs, MapVo rhs) {
                        Date date1 = Util.stringToDate(lhs.getData());
                        Date date2 = Util.stringToDate(rhs.getData());
                        // 对日期字段进行升序，如果欲降序可采用after方法
                        if (date2.before(date1)) {
                            return 1;
                        }
                        return -1;
                    }
                });

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date date = sdf.parse(mlist_gridview.get(0).getData());
                    week = Util.getWeekOfDate(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                if (week.equals("星期一")) {

                } else if (week.equals("星期二")) {

                    MapVo mapVo = new MapVo("", "");
                    mlist_gridview.add(0, mapVo);

                } else if (week.equals("星期三")) {
                    MapVo mapVo = new MapVo("", "");
                    MapVo mapVo1 = new MapVo("", "");
                    mlist_gridview.add(0, mapVo);
                    mlist_gridview.add(1, mapVo1);
                } else if (week.equals("星期四")) {
                    MapVo mapVo = new MapVo("", "");
                    MapVo mapVo1 = new MapVo("", "");
                    MapVo mapVo2 = new MapVo("", "");
                    mlist_gridview.add(0, mapVo);
                    mlist_gridview.add(1, mapVo1);
                    mlist_gridview.add(2, mapVo2);
                } else if (week.equals("星期五")) {
                    MapVo mapVo = new MapVo("", "");
                    MapVo mapVo1 = new MapVo("", "");
                    MapVo mapVo2 = new MapVo("", "");
                    MapVo mapVo3 = new MapVo("", "");
                    mlist_gridview.add(0, mapVo);
                    mlist_gridview.add(1, mapVo1);
                    mlist_gridview.add(2, mapVo2);
                    mlist_gridview.add(3, mapVo3);
                } else if (week.equals("星期六")) {
                    MapVo mapVo = new MapVo("", "");
                    MapVo mapVo1 = new MapVo("", "");
                    MapVo mapVo2 = new MapVo("", "");
                    MapVo mapVo3 = new MapVo("", "");
                    MapVo mapVo4 = new MapVo("", "");
                    mlist_gridview.add(0, mapVo);
                    mlist_gridview.add(1, mapVo1);
                    mlist_gridview.add(2, mapVo2);
                    mlist_gridview.add(3, mapVo3);
                    mlist_gridview.add(4, mapVo4);
                } else if (week.equals("星期日")) {
                    MapVo mapVo = new MapVo("", "");
                    MapVo mapVo1 = new MapVo("", "");
                    MapVo mapVo2 = new MapVo("", "");
                    MapVo mapVo3 = new MapVo("", "");
                    MapVo mapVo4 = new MapVo("", "");
                    MapVo mapVo5 = new MapVo("", "");
                    mlist_gridview.add(0, mapVo);
                    mlist_gridview.add(1, mapVo1);
                    mlist_gridview.add(2, mapVo2);
                    mlist_gridview.add(3, mapVo3);
                    mlist_gridview.add(4, mapVo4);
                    mlist_gridview.add(5, mapVo5);
                }


                GridViewAdapter myAdapter = new GridViewAdapter(mlist_gridview, this);
                health_scheme_gridview.setAdapter(myAdapter);

                lin_xx.setTag(i);//健康方案详情
                lin_disan.setTag(i);//展开
                lin_disan2.setTag(i);//收起
                today_btn.setTag(i);//完成
                lin_disan.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        int pos = (Integer) v.getTag();
                        currentPos = pos;
                        if (lin_yi.getVisibility() == View.VISIBLE) {
                            lin_wei.setVisibility(View.VISIBLE);
                            lin_yi.setVisibility(View.GONE);
                        } else {
                            lin_yi.setVisibility(View.VISIBLE);
                            lin_wei.setVisibility(View.GONE);
                        }
                    }
                });

                lin_disan2.setOnClickListener(new View.OnClickListener() {//收起按钮

                    @Override
                    public void onClick(View v) {
                        int pos = (Integer) v.getTag();
                        currentPos = pos;
                        if (lin_yi.getVisibility() == View.VISIBLE) {
                            lin_wei.setVisibility(View.VISIBLE);
                            lin_yi.setVisibility(View.GONE);
                        } else {
                            lin_yi.setVisibility(View.VISIBLE);
                            lin_wei.setVisibility(View.GONE);
                        }
                    }
                });
                today_btn.setOnClickListener(new View.OnClickListener() {//今日完成按钮

                    @Override
                    public void onClick(View v) {
                        int pos = (Integer) v.getTag();

                        String id = mlist.get(pos).getId();

                        provider.requestUpdateHealthPlanUser(id, "UpdateHealthPlanUser");

                    }
                });


                lin_xx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos2 = (Integer) v.getTag();

                        Bundle bundle = new Bundle();
                        bundle.putString("id", mlist.get(pos2).getId());
                        ActivityUtils.showActivity(HealthSchemeActivity.this, SchemeDetailActivity.class, bundle);

                    }
                });


                //重复运动周期自定义添加view
                String var[] = mlist.get(i).getType().split(",");

                if (mlist.get(i).getType().equals("0")) {
                    chongfu.setText("重复: " + " 每天");
                } else {
                    chongfu.setText("重复 :" + mlist.get(i).getType().replace("1", " 周一").replace("2", " 周二").replace("3", " 周三").replace("4", " 周四").replace("5", " 周五").replace("6", " 周六").replace("7", " 周日"));

                }

                jtjhLin.addView(view);
            }


        } else if (actionName.equals("Comment")) {

            commentVos = (List<CommentVo>) object;

            commentAdapter = new CommentAdapter(commentVos, this);
            comment_listview.setAdapter(commentAdapter);

        }else if (actionName.equals("Get_HealthScheme")) {


            SchemeVo schemeVo = (SchemeVo) object;

            schemetempletid = schemeVo.getSchemetempletid();

            provider.requestCommentList(schemetempletid, page + "", pagesize + "", "Comment");//请求  用户评论列表


            hsTvMs.setText(schemeVo.getDetail());
            hsTieleTs.setText(schemeVo.getDaynum() + "/" + schemeVo.getTotaldaynum() + "天");//多少天 ....
            int a = Integer.parseInt(schemeVo.getDaynum());
            int b = Integer.parseInt(schemeVo.getTotaldaynum());

            int c = a * 100 / b;

            hsTieleBfb.setText(c + "%");

            if(a == b){
                downLin.setVisibility(View.VISIBLE);//计划完成   重新添加  评论按钮显示
            }else{
                downLin.setVisibility(View.GONE);
            }
            hsTitleName.setText(schemeVo.getName());

        }else if (actionName.equals("UpdateHealthPlanUser")) {

            showToast("今日已完成");
            provider.requestAddScore("7", "");//添加积分
            provider.requestHealthScheme(id, "HealthScheme");
            provider.requestCommentList(schemetempletid, page + "", pagesize + "", "Comment");

        } else if (actionName.equals("AddHealth")) {

            showToast("添加方案成功");

        } else if (actionName.equals("SchemeComment")) {

            showToast("评论成功");
            health_comment_lin.setVisibility(View.GONE);
            edit_health_scheme.setText("");//输入框赋值为空

        }
    }
}
